package com.yuanqunsoft.alimigration.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.yuanqunsoft.alimigration.dao.IRuleImplementDao;
import com.yuanqunsoft.alimigration.model.Rule;
import com.yuanqunsoft.alimigration.model.RuleImplement;

public class RuleImplementDaoImpl extends HibernateDaoSupport implements IRuleImplementDao {
  
    private int currentPage;
    private int pageSize;
    private int allPage;
    @Override
    public List<Rule> getAllRules() {
        // TODO Auto-generated method stub
        List<Rule> rules = new ArrayList<Rule>();
        String hql = "from Rule";
        rules = (List<Rule>)this.getHibernateTemplate().find(hql);
        return rules;
    }

    @Override
    public boolean save(RuleImplement rs) {
        // TODO Auto-generated method stub
        try{
            this.getHibernateTemplate().save(rs);
            this.getHibernateTemplate().flush();

        }catch(Exception e){
            return false;
        }
        return true;
    }

    public boolean delete(RuleImplement ruleImplement) {
        // TODO Auto-generated method stub
        try{
           
            this.getHibernateTemplate().delete(ruleImplement);
        }catch(Exception e){
            return false;
        }
        return true;
    }


    @Override
    public boolean update(RuleImplement rs) {
        // TODO Auto-generated method stub
        try{
            this.getHibernateTemplate().update(rs);
        }catch(Exception e){
            return false;
        }
        return true;
    }

    @Override
    public List<RuleImplement> getAllRuleImplement() {
        // TODO Auto-generated method stub
        String hql = "from RuleImplement";
        return this.getHibernateTemplate().find(hql);
    }

    @Override
    public List<RuleImplement> queryPage(final int currentPage, final int pageSize) {
        // TODO Auto-generated method stub
        this.currentPage=currentPage;
        this.pageSize=pageSize;
        
        return (List<RuleImplement>)this.getHibernateTemplate().execute(new HibernateCallback() {

            @Override
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                // TODO Auto-generated method stub
                String hql = "from RuleImplement";
                Query q = session.createQuery(hql);
                List<RuleImplement> r1 = q.list();
                allPage = r1.size();
                q = session.createQuery(hql);
                q.setMaxResults(RuleImplementDaoImpl.this.pageSize);
                q.setFirstResult(RuleImplementDaoImpl.this.currentPage);
                return q.list();
            }
            
        });
    }

    @Override
    public RuleImplement getRuleImplementById(int id) {
        // TODO Auto-generated method stub
        return this.getHibernateTemplate().get(RuleImplement.class, id);
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getAllPage() {
        return allPage;
    }

    public void setAllPage(int allPage) {
        this.allPage = allPage;
    }

    @Override
    public List<RuleImplement> getRuleImplementByRuleId(int id) {
        // TODO Auto-generated method stub
        String query = "from RuleImplement r where r.rule.rulId=?";
        List<RuleImplement> result = getHibernateTemplate().find(query,id);
        return result;
    }

    @Override
    public Rule getRuleById(int id) {
        // TODO Auto-generated method stub
        return (Rule)this.getHibernateTemplate().get(Rule.class, id);
    }

    
}
